<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 奖项列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" class="fr" @click="addVisible = true">添加奖项</el-button>
      </div>
      <el-table
        :data="prizeData"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID" sortable width="70" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" width="120" align="center"></el-table-column>
        <el-table-column prop="num" label="抽取人数" width="100" align="center"></el-table-column>
        <el-table-column prop="per" label="每页人数" width="100" align="center"></el-table-column>
        <el-table-column prop="sort" label="排序" width="120" align="center"></el-table-column>
        <el-table-column prop="create_time" label="添加时间" align="center"></el-table-column>
        <el-table-column label="是否启用" width="120" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="setStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope)">编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="35%">
      <el-form ref="form" :model="editForm" label-width="70px">
        <el-form-item label="名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="抽取人数">
          <el-input v-model="editForm.num"></el-input>
        </el-form-item>
        <el-form-item label="每页人数">
          <el-input v-model="editForm.per"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加弹出框 -->
    <el-dialog title="添加" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="addForm" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="抽取人数">
          <el-input v-model="addForm.num"></el-input>
        </el-form-item>
        <el-form-item label="每页人数">
          <el-input v-model="addForm.per"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPrize">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" top='30vh' center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/userApi.js";
export default {
  name: 'basetable',
  data() {
    return {
      url: './static/vuetable.json',
      prizeData: [],
      editVisible: false,
      delVisible: false,
      addVisible: false,
      form: {
        name: '',
        date: '',
        address: ''
      },
      addForm: {  //添加奖项
        name: '',
        num: null,
        per: null
      },
      editForm: {
        id: '',
        name: '',
        num: null,
        per: null
      },
      editIndex: 0,
      setId: 0,  //编辑或删除的暂存ID
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      api.getPrizeList()
        .then((res) => {
          this.prizeData = res.data.list;
          this.prizeData.forEach(d => {
            if (d.status == 0) {
              d.status = true
            } else {
              d.status = false
            }
          })
        })
    },
    setStatus(row) {
      const id = row.id
      api.statusPrize({
        id: id
      })
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.$message.success('修改成功')
          console.log(res);
          if (res.data.status == 0) {
            row.status = true
          } else {
            row.status = false
          }
        })
    },
    handleEdit(item) {
      this.editIndex = item.$index
      const data = item.row
      this.editForm = {
        id: data.id,
        name: data.name,
        num: data.num,
        per: data.per
      }
      this.editVisible = true;
    },
    handleDelete(index, row) {
      this.idx = index;
      this.setId = row.id
      this.delVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 保存编辑
    saveEdit() {
      api.editPrize(this.editForm)
        .then((res) => {
          console.log(res);
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.getData()
          this.editVisible = false;
          this.$message.success('修改成功');
        })
    },
    // 确定删除
    deleteRow() {
      api.deletePrize({
        id: this.setId
      }).then(res => {
        if (res.code) {
          this.$message.error(res.message)
          return;
        }
        this.$message.success('删除成功')
        this.prizeData.splice(this.idx, 1);
        this.delVisible = false;
      })
    },
    // 添加奖项
    addPrize() {
      const data = this.addForm;
      api.addPrize(data)
        .then((res) => {
          console.log(res);
          this.addForm = [];
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.getData()
          this.$forceUpdate()
          this.$message.success('添加成功');
          this.addVisible = false
        })
    }
  }
}

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
  overflow: hidden;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
</style>
